﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>1.0.User表的增删改</title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css"/>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>配置控制器.</h3>
    <b>
	 1.添加数据库连接：
    </b>
    <pre class="brush: csharp">
        //数据库类型定义
		public class BaseController:NFinal.OwinAction&lt;NFinal.User.User>
        {
            /// &lt;summary>
            /// 获取连接字符串
            /// &lt;/summary>
            public override IDbConnection GetDbConnection()
            {
                //读取plug.json中的配置信息
                string connectionString=this.config.connectionStrings["Common"].connectionString;
                System.Data.IDbConnection con=new System.Data.SqlClient.SqlConnection(connectionString);
                return con;
            }
        }
        //使用方法
        public class IndexController:BaseController
        {
            public void Index(Models.User user)
            {
                //快速更新，快速插入，快速查询等函数。
                bool result= this.Con.SimpleUpdate(user,"where id="+user.id);
                this.AjaxReturn(result);
            }
        }
    </pre>
    <b>
        2.配置Session:
    </b>
    <pre class="brush: csharp;">
        //Session方式配置
        //1.引入依赖注入命名空间
        using NFinal.DependencyInjection;
        //2.在Startup的Configure配置函数中添加全局依赖注入
        public class Startup
        {
            //在appStart的configure函数中添加
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                //2.a.设置以redis为缓存的Session
                NFinal.Config.Configration.serviceCollection.SetRedisSession(30,"localhost");
                //2.b.设置以内存为缓存的Session
                NFinal.Config.Configration.serviceCollection.SetSimpleSession(30);
            }
        }
        //使用方法
        public class IndexController:BaseController
        {
            public void Index()
            {
                //1.与asp.net 相同的session使用方式
                Session["count"]=1;
                //2.从Session中获取用户数据
                Code.User user= Session.GetUser&lt;Code.User>();
                //3.设置用户数据
                user.name=1;
                //4.把用户数据存入Session
                Session.SetUser(user);
            }
        }
    </pre>
    <b>
        3.配置全站参数
    </b>
    <pre class="brush: csharp;">
        //配置全局参数
        public class BaseController:NFinal.OwinAction&lt;NFinal.User.User>
        {
            /// &lt;summary>
            /// 全局配置函数
            /// &lt;/summary>
            public static void Configure(NFinal.Config.Plug.PlugConfig plugConfig)
            {
                Dictionary&lt;string, StringContainer> systemConfigDictionary = new Dictionary&lt;string, StringContainer>();
                //此处可从数据库加载相关数据做为网站全局配置参数
                systemConfigDictionary.Add("siteName", "站点名称");
                systemConfigDictionary.Add("mobile","联系电话");
                //快速搜索函数
                plugConfig.keyValueCache = new NFinal.Collections.FastSearch.FastSearch&lt;StringContainer>(systemConfigDictionary);
                systemConfigDictionary.Clear();
            }
        }
    </pre>
</body>
</html>